#include "Timer.h"

void Timer::Restart()
{
    passTime = 0;
    shoted = false;
}

void Timer::SetWaitTime(double val)
{
    waitTime = val;
}

void Timer::SetOneShot(bool flag)
{
    oneShot = flag;
}

void Timer::SetOnTimeout(std::function<void()> onTimeout)
{
    this->onTimeout = std::move(onTimeout);
}

void Timer::Pause()
{
    paused = true;
}

void Timer::Resume()
{
    paused = false;
}

void Timer::OnUpdate(double delta)
{
    if (paused) return;

    passTime += delta;
    if (passTime >= waitTime) {
        bool can_shot = (!oneShot || (oneShot && !shoted));
        shoted = true;
        if (can_shot && onTimeout)
            onTimeout();

        passTime -= waitTime;
    }
}
